<template>
	<view class="address-list-page">
		<!-- 顶部导航栏 -->
		<view class="top-nav">
			<view class="nav-back" @click="goBack">
				<text class="back-icon">‹</text>
			</view>
			<text class="nav-title">收货地址</text>
			<view class="nav-placeholder"></view>
		</view>
		
		<!-- 地址列表 -->
		<view class="address-content">
			<view class="address-item" v-for="(item, index) in addressList" :key="index">
				<view class="address-main" @click="selectAddress(item)">
					<view class="address-header">
						<text class="receiver-name">{{ item.receiverName }}</text>
						<text class="receiver-phone">{{ item.receiverPhone }}</text>
						<view class="address-tag" v-if="item.isDefault">
							<text class="tag-text">默认</text>
						</view>
					</view>
					<view class="address-detail">
						<text class="address-text">{{ item.province }}{{ item.city }}{{ item.district }}{{ item.detailAddress }}</text>
					</view>
				</view>
				<view class="address-actions">
					<view class="action-item" @click.stop="setDefault(item, index)">
						<text class="action-icon" :class="{active: item.isDefault}">✓</text>
						<text class="action-text" :class="{active: item.isDefault}">设为默认</text>
					</view>
					<view class="action-item" @click.stop="editAddress(item, index)">
						<text class="action-icon">✎</text>
						<text class="action-text">编辑</text>
					</view>
					<view class="action-item" @click.stop="deleteAddress(item, index)">
						<text class="action-icon">🗑</text>
						<text class="action-text">删除</text>
					</view>
				</view>
			</view>
			
			<!-- 空状态 -->
			<view class="empty-state" v-if="addressList.length === 0">
				<image src="/static/组 9@3x(9).png" class="empty-icon" mode="aspectFit" />
				<text class="empty-text">暂无收货地址</text>
				<text class="empty-tip">点击下方按钮添加地址</text>
			</view>
		</view>
		
		<!-- 添加地址按钮 -->
		<view class="add-address-bar">
			<view class="add-btn" @click="addAddress">
				<text class="add-icon">+</text>
				<text class="add-text">添加新地址</text>
			</view>
		</view>
		
		<!-- 删除确认弹窗 -->
		<view class="delete-dialog-overlay" v-if="showDeleteDialog" @click.stop="hideDeleteDialog">
			<view class="delete-dialog-content" @click.stop>
				<view class="dialog-title">确认删除该地址吗？</view>
				<view class="dialog-buttons">
					<view class="dialog-btn cancel-btn" @click.stop="hideDeleteDialog">取消</view>
					<view class="dialog-btn confirm-btn" @click.stop="confirmDelete">确认</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showDeleteDialog: false, // 删除确认弹窗显示状态
				deleteIndex: -1, // 待删除的地址索引
				addressList: [
					{
						id: 1,
						receiverName: '张三',
						receiverPhone: '13800138000',
						province: '河南省',
						city: '郑州市',
						district: '金水区',
						detailAddress: '花园路100号',
						isDefault: true
					},
					{
						id: 2,
						receiverName: '李四',
						receiverPhone: '13900139000',
						province: '河南省',
						city: '洛阳市',
						district: '西工区',
						detailAddress: '中州路200号',
						isDefault: false
					},
					{
						id: 3,
						receiverName: '王五',
						receiverPhone: '13700137000',
						province: '河南省',
						city: '开封市',
						district: '龙亭区',
						detailAddress: '中山路300号',
						isDefault: false
					}
				]
			}
		},
		onLoad() {
			// 从本地存储加载地址列表
			this.loadAddressList();
		},
		onShow() {
			// 页面显示时重新加载地址列表（可能从编辑页返回）
			this.loadAddressList();
			// 确保弹窗状态重置
			this.showDeleteDialog = false;
			this.deleteIndex = -1;
		},
		methods: {
			// 返回上一页
			goBack() {
				uni.navigateBack();
			},
			// 加载地址列表
			loadAddressList() {
				// 从本地存储读取地址列表
				const savedList = uni.getStorageSync('addressList');
				if (savedList && savedList.length > 0) {
					this.addressList = savedList;
				}
				// 确保弹窗状态重置
				this.showDeleteDialog = false;
				this.deleteIndex = -1;
			},
			// 保存地址列表到本地存储
			saveAddressList() {
				uni.setStorageSync('addressList', this.addressList);
			},
			// 选择地址（用于订单确认等场景）
			selectAddress(item) {
				// 可以在这里处理选择地址的逻辑
				// 例如：返回上一页并传递选中的地址
				// const pages = getCurrentPages();
				// if (pages.length > 1) {
				// 	const prevPage = pages[pages.length - 2];
				// 	prevPage.selectedAddress = item;
				// 	uni.navigateBack();
				// }
			},
			// 设为默认地址
			setDefault(item, index) {
				// 将所有地址的默认状态设为false
				this.addressList.forEach(addr => {
					addr.isDefault = false;
				});
				// 设置当前地址为默认
				item.isDefault = true;
				this.saveAddressList();
				uni.showToast({
					title: '已设为默认地址',
					icon: 'success'
				});
			},
			// 编辑地址
			editAddress(item, index) {
				uni.navigateTo({
					url: `/pages/my/addAddress?action=edit&index=${index}&addressData=${encodeURIComponent(JSON.stringify(item))}`
				});
			},
			// 删除地址
			deleteAddress(item, index) {
				this.deleteIndex = index;
				this.showDeleteDialog = true;
			},
			// 隐藏删除确认弹窗
			hideDeleteDialog() {
				this.showDeleteDialog = false;
				this.deleteIndex = -1;
			},
			// 确认删除地址
			confirmDelete() {
				if (this.deleteIndex >= 0) {
					const deletedItem = this.addressList[this.deleteIndex];
					// 如果删除的是默认地址，且还有其他地址，则设置第一个为默认
					if (deletedItem.isDefault && this.addressList.length > 1) {
						const newIndex = this.deleteIndex === 0 ? 1 : 0;
						if (this.addressList[newIndex]) {
							this.addressList[newIndex].isDefault = true;
						}
					}
					// 删除地址
					this.addressList.splice(this.deleteIndex, 1);
					this.saveAddressList();
					uni.showToast({
						title: '删除成功',
						icon: 'success'
					});
				}
				this.hideDeleteDialog();
			},
			// 添加新地址
			addAddress() {
				uni.navigateTo({
					url: '/pages/my/addAddress?action=add'
				});
			}
		}
	}
</script>

<style scoped>
	.address-list-page {
		background-color: #f5f5f5;
		min-height: 100vh;
		padding-bottom: 120rpx;
	}

	/* 顶部导航栏 */
	.top-nav {
		background-color: #2a6df6;
		background-image: url('/static/图层 1@3x.png');
		background-size: cover;
		background-repeat: no-repeat;
		background-position: center;
		color: #fff;
		height: 44px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: relative;
		padding: 0 30rpx;
		box-sizing: border-box;
	}

	.nav-back {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.back-icon {
		color: #fff;
		font-size: 48rpx;
		font-weight: 300;
		line-height: 1;
	}

	.nav-title {
		font-size: 18px;
		font-weight: bold;
		flex: 1;
		text-align: center;
	}

	.nav-placeholder {
		width: 60rpx;
	}

	/* 地址列表 */
	.address-content {
		padding: 20rpx;
	}

	.address-item {
		background-color: #fff;
		border-radius: 16rpx;
		margin-bottom: 20rpx;
		overflow: hidden;
	}

	.address-main {
		padding: 30rpx;
		border-bottom: 1px solid #f0f0f0;
	}

	.address-header {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.receiver-name {
		font-size: 32rpx;
		font-weight: 500;
		color: #333;
		margin-right: 20rpx;
	}

	.receiver-phone {
		font-size: 28rpx;
		color: #666;
		flex: 1;
	}

	.address-tag {
		background-color: #ff4d4f;
		border-radius: 4rpx;
		padding: 4rpx 12rpx;
	}

	.tag-text {
		font-size: 20rpx;
		color: #fff;
	}

	.address-detail {
		margin-top: 10rpx;
	}

	.address-text {
		font-size: 28rpx;
		color: #666;
		line-height: 1.6;
	}

	.address-actions {
		display: flex;
		align-items: center;
		justify-content: space-around;
		padding: 20rpx 30rpx;
		border-top: 1px solid #f0f0f0;
	}

	.action-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: 8rpx;
	}

	.action-icon {
		font-size: 36rpx;
		color: #999;
		line-height: 1;
	}

	.action-icon.active {
		color: #2a6df6;
	}

	.action-text {
		font-size: 24rpx;
		color: #666;
	}

	.action-text.active {
		color: #2a6df6;
	}

	/* 空状态 */
	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 150rpx 0;
	}

	.empty-icon {
		width: 200rpx;
		height: 200rpx;
		opacity: 0.3;
		margin-bottom: 30rpx;
	}

	.empty-text {
		font-size: 32rpx;
		color: #999;
		margin-bottom: 10rpx;
	}

	.empty-tip {
		font-size: 28rpx;
		color: #ccc;
	}

	/* 添加地址按钮栏 */
	.add-address-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: #fff;
		padding: 20rpx 30rpx;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
		box-shadow: 0 -2rpx 8rpx rgba(0, 0, 0, 0.1);
		z-index: 10;
	}

	.add-btn {
		width: 100%;
		background-color: #2a6df6;
		color: #fff;
		text-align: center;
		padding: 24rpx 0;
		border-radius: 12rpx;
		font-size: 32rpx;
		font-weight: 500;
		display: flex;
		align-items: center;
		justify-content: center;
		gap: 10rpx;
	}

	.add-icon {
		font-size: 40rpx;
		line-height: 1;
	}

	.add-text {
		font-size: 32rpx;
	}

	/* 删除确认弹窗 */
	.delete-dialog-overlay {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 9999;
	}

	.delete-dialog-content {
		background-color: #fff;
		border-radius: 16rpx;
		width: 80%;
		max-width: 600rpx;
		overflow: hidden;
		position: relative;
		z-index: 10000;
	}

	.dialog-title {
		text-align: center;
		padding: 60rpx 30rpx 40rpx;
		font-size: 32rpx;
		color: #333;
		font-weight: 500;
	}

	.dialog-buttons {
		display: flex;
		border-top: 1px solid #f0f0f0;
	}

	.dialog-btn {
		flex: 1;
		text-align: center;
		padding: 30rpx 0;
		font-size: 32rpx;
		position: relative;
		z-index: 10001;
	}

	.cancel-btn {
		color: #666;
		border-right: 1px solid #f0f0f0;
	}

	.confirm-btn {
		color: #ff4d4f;
		font-weight: 500;
	}
</style>
